package com.zz.ecommerce.utils;

import org.apache.commons.lang3.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * <һ�仰��������>
 * <������ϸ����>
 *
 * @author STEVEN
 * @date 2015/9/24,14:06
 */
public class DateUtil {

    private static SimpleDateFormat weekFormat = new SimpleDateFormat("E", Locale.CHINESE);

    /**
     * ������ yyyy-MM-dd
     *
     * @param date
     * @return
     */
    public static String getDate(Date date) {
        return DateFormat.getDateInstance().format(date);
    }

    /**
     * ������ yyyy-MM-dd hh:mm:ss
     *
     * @param date
     * @return
     */
    public static String getDateTimeStr(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(StringUtils.isEmpty(format) ? "yyyy-MM-dd hh:mm:ss" : format);
        return sdf.format(date);
    }

    /**
     * ������ yyyy-MM-dd hh:mm:ss
     *
     * @param date
     * @return
     */
    public static String getDateTimeStr(Date date) {
        return getDateTimeStr(date, null);
    }

    /**
     * ������ yyyy-MM-dd
     *
     * @param date
     * @return
     */
    public static String getWeek(Date date) {
        return weekFormat.format(date);
    }

    /**
     * ������ yyyy-MM-dd
     *
     * @param date
     * @return
     */
    public static String getTime(Date date) {
        return DateFormat.getTimeInstance().format(date);
    }

    /**
     * ������ yyyy-MM-dd HH:mm:ss
     *
     * @param format
     * @return
     */
    public static String getCurrentTime(String format) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        SimpleDateFormat dateFormat = new SimpleDateFormat(format, Locale.CHINA);
        return dateFormat.format(cal.getTime());
    }

    /**
     * ������ yyyy-MM-dd HH:mm:ss
     *
     * @param format
     * @return
     */
    public static String getMiniMonthTime(String format) {
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        SimpleDateFormat dateFormat = new SimpleDateFormat(format, Locale.CHINA);
        return dateFormat.format(calendar.getTime());
    }

    public static String max(String dateStr1, String dateStr2) {
        if (StringUtils.isEmpty(dateStr1) || StringUtils.isEmpty(dateStr2)) {
            return ZzStringUtil.getNowTime("yyyy-MM-dd HH:mm:ss");
        }

        return compareTo(dateStr1, dateStr2) ? dateStr1 : dateStr2;
    }

    public static String min(String dateStr1, String dateStr2) {
        dateStr1 = StringUtils.defaultIfEmpty(dateStr1,dateStr2);
        dateStr2 = StringUtils.defaultIfEmpty(dateStr2,dateStr1);

        return compareTo(dateStr1, dateStr2) ? dateStr2 : dateStr1;
    }

    public static boolean compareTo(String dateStr1, String dateStr2) {
        if (StringUtils.isEmpty(dateStr1)) {
            return false;
        }

        if (StringUtils.isEmpty(dateStr2)) {
            return true;
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();

        try {
            cal1.setTime(sdf.parse(dateStr1));
            cal2.setTime(sdf.parse(dateStr2));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return cal1.compareTo(cal2) > 0;
    }

    public static String getIntervalDays(String startDateStr, String endDateStr) {
        return getIntervalDays(startDateStr, endDateStr, null);
    }

    public static String getIntervalDays(String startDateStr, String endDateStr, String format) {
        if(StringUtils.isEmpty(startDateStr)){
            return "0 Сʱ";
        }

        int dayCnt = 0;
        int hourCnt = 0;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(StringUtils.isEmpty(format) ? "yyyy-MM-dd HH:mm:ss" : format);

            Date startDate = sdf.parse(startDateStr);
            Calendar start = Calendar.getInstance();
            start.setTime(startDate);

            Calendar end = Calendar.getInstance();
            if (StringUtils.isNotEmpty(endDateStr)) {
                Date endDate = sdf.parse(endDateStr);
                end.setTime(endDate);
            }

            //�ֱ�õ�����ʱ��ĺ�����
            long sl = start.getTimeInMillis();
            long el = end.getTimeInMillis();
            long ei = el - sl;
            //���ݺ���������������

            dayCnt = (int) (ei / (1000 * 60 * 60 * 24));
            hourCnt = (int) ((ei % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));

        } catch (Exception e) {
            e.printStackTrace();
        }

        return (dayCnt == 0 ? "" : dayCnt + "��") + hourCnt + "Сʱ";
    }


    /**
     * 年月日 yyyy-MM-dd hh:mm:ss
     *
     * @return
     */
    public static String nowTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(new Date());
    }

    /**
     * 年月日 yyyy-MM-ddThh:mm:ss
     *
     * @return
     */
    public static String nowTimeQingdaoFileCreate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss");
        return sdf.format(new Date());
    }

    /**
     * yyyyMMddHHmmssfff
     *
     * @return
     */
    public static String nowTimeQingdaoFileName() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss'fff'");
        return sdf.format(new Date());
    }
}
